package com.shoe.controller;

import com.shoe.pojo.AdminCouponPojo;
import com.shoe.pojo.UserGetCouPojo;
import com.shoe.service.UCouponService;
import com.shoe.util.DateConversion;
import com.shoe.util.ResultUtil;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UCouponController {
    @Autowired
    UCouponService uCouponService;

    @Autowired
    ResultUtil resultUtil;

    @GetMapping("selectUCouHave")
    public ResponseResult selectUCouHave(Integer userId, Integer userCouId) {
        UserGetCouPojo uCouHaveIdHaveId = uCouponService.selectUCouHave(userId, userCouId);

        System.out.println(uCouHaveIdHaveId);

        if (null == uCouHaveIdHaveId) {
            return resultUtil.getResponseResult(0, "未领取", uCouHaveIdHaveId);
        } else {
            return resultUtil.getResponseResult(2, "已取",uCouHaveIdHaveId);
        }
    }

    @GetMapping("selectUCoupon")
    public ResponseResult selectUCouponByCondition(@RequestParam(defaultValue = "1") Integer currPage, @RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "4") Integer userId, String CType, Integer CMoney, Integer CHolding) {
        return uCouponService.selectUCouponByCondition(currPage, pageSize, userId, CType, CMoney, CHolding);
    }

    @GetMapping("selectUCouShow")
    public ResponseResult selectUCouShow(Integer couponId) {
        List<AdminCouponPojo> selectUCouShow = uCouponService.selectUCouShow(couponId);
        if (null != selectUCouShow && selectUCouShow.size() > 0) {
            return resultUtil.getResponseResult(0, "", selectUCouShow.size(), selectUCouShow);
        } else {
            return resultUtil.getResponseResult(2, "查询失败");
        }
    }

    @GetMapping("addUGetCou")
    public ResponseResult addUGetCou(Integer couponId, Integer userId) {

//        DateConversion dateConversion = new DateConversion();
//        System.out.println(dateConversion.Data2Sting());

        UserGetCouPojo userGetCouPojo = new UserGetCouPojo();
        userGetCouPojo.setUsercouponDate(setUCouHoldDay(couponId));
        userGetCouPojo.setUsercouponCouponId(couponId);
        userGetCouPojo.setUsercouponUserId(userId);

        System.out.println(userGetCouPojo.toString());

        int affRow = uCouponService.addUGetCou(userGetCouPojo);
        if (affRow > 0) {
            return resultUtil.getResponseResult(0, "", affRow);
        } else {
            return resultUtil.getResponseResult(2, "领取失败");
        }

    }

    @GetMapping("delUCou")
    public ResponseResult deleUCou(Integer userCouId) {
        int deleUCouAffRow = uCouponService.deleUCou(userCouId);
        if (deleUCouAffRow > 0) {
            return resultUtil.getResponseResult(0, "", deleUCouAffRow);
        } else {
            return resultUtil.getResponseResult(2, "删除失败");
        }
    }

    @GetMapping("delucoudate")
    public ResponseResult delUCouDate(Integer userId) {
        return uCouponService.delUCouDate(userId);
    }

    public String setUCouHoldDay(Integer couponId) {
        List<AdminCouponPojo> seleCouHole = uCouponService.selectUCouShow(couponId);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, +seleCouHole.get(0).getCouponHolding());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(cal.getTime());
    }


}
